// wigets/business/addInvoice/index.js
import {
  api,
  util,
  forms,
  dialog
} from '../../../js/util';
import {
  invoice
} from '../../../js/modules/store';
const componentBehavior = require('../../../js/common/ComponentBehavior')

Component({
  /**
   * 组件的初始数据
   */
  data: {
    invoiceInfo: {},
    invoiceTypeItems: [
      {
        value: '增值税发票',
        checked: 'checked'
      },
      {
        value: '普通发票'
      }
    ],
    editStatus: false, //操作状态
  },
  behaviors: [componentBehavior],
  /**
   * 组件的方法列表
   */
  methods: {
    /**文本框触发 */
    bindInputChange(e) {
      var invoiceInfo = this.data.invoiceInfo;
      invoiceInfo[e.currentTarget.id] = e.detail.value;
      this.setData({
        invoiceInfo: invoiceInfo
      });
    },
    /**Switch触发 */
    bindSwitchChange(e){
      var invoiceInfo = this.data.invoiceInfo;
      invoiceInfo[e.currentTarget.id] = e.detail.value ? 1 : 0;
      this.setData({
        invoiceInfo: invoiceInfo
      });
    },
    /**
     * 切换发票类型
     */
    changeInvoiceType: function (e) {
      var invoiceInfo = this.data.invoiceInfo;
      this.data.invoiceTypeItems.map(function (item) {
        if (item.value == e.detail.value) {
          item.checked = true;
        } else {
          item.checked = false;
        }
      });
      if (e.detail.value=='增值税发票') {
        invoiceInfo.InvType = 2;
      } else {
        invoiceInfo.InvType = 1;
      }
      this.setData({
        invoiceInfo: invoiceInfo,
        invoiceTypeItems: this.data.invoiceTypeItems
      })
    },
    /**显示发票弹层 */
    showAddModal(invoiceInfo) {
      let that = this;
      var invoiceTypeItems = this.data.invoiceTypeItems;
      if (invoiceInfo.Id > 0) {
        invoice.invoiceInfo(invoiceInfo.Id,function (status, res) {
          if (status) {
            invoiceTypeItems.map(function (item) {
              if (item.value == '增值税发票'&&res.InvType==2) {
                item.checked = true;
              }else if (item.value == '普通发票'&&res.InvType==1) {
                item.checked = true;
              } else {
                item.checked = false;
              }
            });
            that.setData({
              invoiceInfo: res,
              invoiceTypeItems: invoiceTypeItems
            });
            dialog.showModal(that, 'showModalStatus')
          }else{
            util.tips(res||"暂无权限修改");
          }
        });
      } else {
        invoiceTypeItems.map(function (item) {
          if (item.value == '增值税发票'&&invoiceInfo.InvType==2) {
            item.checked = true;
          }else if (item.value == '普通发票'&&invoiceInfo.InvType==1) {
            item.checked = true;
          } else {
            item.checked = false;
          }
        });
        this.setData({
          invoiceInfo: invoiceInfo,
          invoiceTypeItems: invoiceTypeItems
        });
        dialog.showModal(this, 'showModalStatus')
      }
    },
    /**隐藏发票弹层 */
    closeAddModal() {
      dialog.hideModal(this, 'showModalStatus')
    },
    /**保存发票信息 */
    saveInvoice() {
      var that = this;
      var invoiceInfo = this.data.invoiceInfo;
      if (invoiceInfo.Name == undefined || invoiceInfo.Name == "") {
        util.tips("请填写单位名称");
        return;
      }
      if (invoiceInfo.Code == undefined || invoiceInfo.Code == "") {
        util.tips("请填写税号");
        return;
      }
      if (invoiceInfo.InvType == 2 && (invoiceInfo.PostAddress == undefined || invoiceInfo.PostAddress == "")) {
        util.tips("请填写寄送地址");
        return;
      }
      if (invoiceInfo.InvType == 2 && invoiceInfo.Tel != undefined && invoiceInfo.Tel != "") {
        var mobileItem = {
          value: util.trim(invoiceInfo.Tel),
          validate: "tel",
          error: "请输入正确的电话号码"
        }
        if (!forms.validate(mobileItem)) return false;
      }
      //设置为编辑状态 防止重复提交
      if (this.data.editStatus) return;
      this.setData({
        editStatus: true
      });
      //增加或修改
      invoice.saveInvoice(invoiceInfo, function (status, res) {
        that.setData({
          editStatus: false
        });
        if (status) {
          util.success("保存成功", () => {
            that.triggerEvent("onInvoiceCallbackEvent", { invoiceInfo: invoiceInfo });
            that.closeAddModal();
          });
        } else {
          util.tips(res||"保存失败");
        }
      });
      dialog.hideModal(this, 'showModalStatus')
    },
  }
})
